package com.pengshun.modules.api.news.service.impl;

import com.pengshun.modules.api.coal.repository.UserRepository;
import com.pengshun.modules.api.mapper.NewsFlashMapper;
import com.pengshun.modules.api.mapper.SystemMapper;
import com.pengshun.modules.api.news.service.NewsFlashService;
import com.pengshun.modules.api.news.service.dto.CategoryDto;
import com.pengshun.modules.api.news.service.dto.NewsFlashDto;
import com.pengshun.modules.api.news.service.mapstruct.NewsFlashTodo;
import com.pengshun.modules.api.news.service.mapstruct.NewsTodo;
import com.pengshun.modules.api.news.service.repository.NewsFlashRepository;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.common.PagePara;
import com.pengshun.modules.common.UpdateUtil;
import com.pengshun.modules.domain.NewsFlash;
import com.pengshun.modules.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author luoligang
 * @create 2021/4/27
 */
@Service
@RequiredArgsConstructor
public class NewsFlashServiceImpl  implements NewsFlashService {

    private final NewsFlashRepository newsFlashRepository;
    private final NewsFlashTodo newsFlashTodo;
    private final MessageSource messageSource;
    private final NewsFlashMapper newsFlashMapper;
    private final UserRepository userRepository;
    @Override
    public BaseResult save(NewsFlashDto dto) {
        User user=userRepository.findById(dto.getUserId()).orElseGet(User::new);
        dto.setCreateTime(new Date());
        dto.setStatus("1");
        dto.setReadNum(0);
        dto.setReplyNum(0);
        dto.setPraiseNum(0);
        dto.setWechatCircleNum(0);
        dto.setWechatFirendNum(0);
        dto.setMicroblogNum(0);
        dto.setUserImage(user.getAvatarName());
        newsFlashRepository.save(newsFlashTodo.toEntity(dto));
        return new BaseResult(0, messageSource.getMessage("save.success", null, Locale.CHINA));
    }

    @Override
    public BaseResult myselfList(PagePara para) {
        List<NewsFlashDto> list=newsFlashMapper.myselfList(para.getUserId(),para.getStatus(),para.getPage(),para.getSize());
        return new BaseResult(0,list);
    }

    @Override
    public BaseResult myDetil(Long newsFlashId) {
        NewsFlashDto dto=newsFlashMapper.findById(newsFlashId);
        return new BaseResult(0,dto);
    }

    @Override
    public BaseResult category() {
        List<CategoryDto> list= newsFlashMapper.findNewsFlashCategorys();
        return  new BaseResult(0,list);
    }

    @Override
    public BaseResult list(PagePara para) {
        List<NewsFlashDto> list=newsFlashMapper.findNewsFlashList(para.getStatus(),para.getPage(),para.getSize());
        for (NewsFlashDto dto : list) {
            if(dto.getType().equals("1")){
                //快讯 如果有图片 类型为0 没有为1
                dto.setType(dto.getImages()!=null?"0":"1");
            }
        }
        return new BaseResult(0,list);
    }

    @Override
    public BaseResult detil(Long newsFlashId) {
        NewsFlashDto dto=newsFlashMapper.findById(newsFlashId);
        //更新阅读量
        dto.setReadNum(dto.getReadNum()+1);
        newsFlashRepository.save(newsFlashTodo.toEntity(dto));
        //查询相关快讯
        List<NewsFlashDto> list=newsFlashMapper.findNewsFlashAboutList(dto.getType(),dto.getNewsFlashId());
        for (NewsFlashDto dt : list) {
            if(dt.getType().equals("1")){
                //快讯 如果有图片 类型为0 没有为1
                dt.setType(dt.getImages()!=null?"0":"1");
            }
        }
        dto.setDtos(list);
        return new BaseResult(0,dto);
    }

    @Override
    public BaseResult update(NewsFlashDto dto) {
        NewsFlash flash=newsFlashRepository.findById(dto.getNewsFlashId()).orElseGet(NewsFlash::new);
        if("3".equals(dto.getStatus())){
            //修改
            BeanUtils.copyProperties(dto, flash, UpdateUtil.getNullPropertyNames(dto));
        }else {
            flash.setStatus(dto.getStatus());
        }
        if("2".equals(flash.getStatus())){
            //发布待审核
            flash.setIssueTime(new Date());
        }
        newsFlashRepository.save(flash);
        return new BaseResult(0, messageSource.getMessage("operation.success", null, Locale.CHINA));
    }
}
